﻿@{
    ViewData["Title"] = "新建表";
}
<div class="layui-layout layui-layout-admin">
    <form class="layui-form" action="" lay-filter="val-filter">
        <input type="hidden" name="id" value="0" />
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">库定位器</label>
                <div class="layui-input-inline">
                    <select name="configId" id="configId" lay-filter="configId">
                        <option value="">请选择...</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">库类型</label>
                <div class="layui-input-inline">
                    <select name="dbType" id="dbType" disabled>
                        <option value="0">MySql</option>
                        <option value="1">SqlServer</option>
                        <option value="2">Sqlite</option>
                        <option value="3">Oracle</option>
                        <option value="4">PostgreSQL</option>
                        <option value="5">Dm</option>
                        <option value="6">Kdbndp</option>
                        <option value="7">Oscar</option>
                        <option value="8">MySqlConnector</option>
                        <option value="9">Access</option>
                        <option value="10">OpenGauss</option>
                        <option value="11">QuestDB</option>
                        <option value="12">HG</option>
                        <option value="13">ClickHouse</option>
                        <option value="14"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库地址</label>
            <div class="layui-input-block">
                <textarea disabled name="connectionString" id="connectionString" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">生成表</label>
                <div class="layui-input-inline">
                    <select name="tableName" id="tableName">
                        <option value="">请选择...</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">命名空间</label>
                <div class="layui-input-inline">
                    <select name="nameSpace">
                        <option value="Admin.NET.Application">Admin.NET.Application</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">业务名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="busName" autocomplete="off" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">是否生成菜单</label>
                <div class="layui-input-inline">
                    <select name="generateMenu" id="generateMenu">
                        <option value="true">是</option>
                        <option value="false">否</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">父级菜单</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="menuPid" id="menuPid" value="0">
                    <input type="text" name="menu" id="menu" lay-filter="menu" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width:40px">图标</label>
                <div class="layui-input-block" style="width:260px;margin-left:70px">
                    <input type="text" name="menuIcon" id="menuIcon" lay-filter="menuIcon" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">前端目录</label>
                <div class="layui-input-inline">
                    <input type="text" name="pagePath" autocomplete="off" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">生成方式</label>
                <div class="layui-input-inline">
                    <select name="generateType">
                        <option value="100">下载压缩包</option>
                        <option value="111">下载压缩包(前端)</option>
                        <option value="121">下载压缩包(后端)</option>
                        <option value="200" selected>生成到本项目</option>
                        <option value="211">生成到本项目(前端)</option>
                        <option value="221">生成到本项目(后端)</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">作者姓名</label>
            <div class="layui-input-block">
                <input type="text" name="authorName" autocomplete="off" class="layui-input">
            </div>
        </div>

    </form>
</div>

@section scripts {
    <script type="text/javascript">
        var app = { dbs: [] };
        layui.use(['http', 'toast', 'treeSelect', 'iconPicker'], function () {
            var $ = layui.$, form = layui.form, iconPicker = layui.iconPicker,
                http = layui.http, toast = layui.toast,
                treeSelect = layui.treeSelect;
            //
            app.save = function () {
                form.submit('val-filter', function (data) {
                    var postData = data.field
                    //
                    var url = postData.id == '0' ? '/api/sysCodeGen/add' : '/api/sysCodeGen/update'
                    http.post(url, postData).then((res) => {
                        if (res.code == 200) {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload('lsttable');
                            toast.success({ message: '保存成功！' });
                        }
                        else {
                            layer.alert('保存失败，错误信息:' + res.message, { icon: 2 });
                        }
                    });

                    return false;
                });
            }
            //加载数据库连接并初始化下拉
            app.loadDb = function () {
                return http.get('/api/sysCodeGen/databaseList').then((res) => {
                    app.dbs = res.result;
                    $.each(app.dbs, function (i, item) {
                        $('#configId').append($('<option>').text(item.configId).attr('value', item.configId));
                    });
                    if (app.data.configId != undefined) {
                        $('#configId').val(app.data.configId);
                        app.loadTable(app.data.configId);
                    }
                    form.render("select");
                });
            }
            //加载指定数据库表并初始化下拉
            app.loadTable = function (configId) {
                if (configId !== undefined && configId != '') {
                    let curdb = app.dbs.filter(item => item.configId == configId)[0];
                    console.log(curdb)
                    $('#dbType').val(curdb.dbType);
                    $('#connectionString').val(curdb.connectionString);
                    //加载所有表
                    http.get('/api/sysCodeGen/tableList/' + curdb.configId).then((res) => {
                        $.each(res.result, function (i, item) {
                            $('#tableName').append($('<option>').text(item.entityName + '(' + item.tableName + ')').attr('value', item.entityName));
                        });
                        //
                        $('#tableName').val(app.data.tableName);
                        form.render("select");
                    })
                }
            }
            //加载菜单
            app.loadMenu = function () {
                treeSelect.render({
                    elem: '#menu',
                    data: '/api/sysMenuEx/list',
                    headers: {
                        Authorization: http.getToken()
                    },
                    success: function (d) {
                        // 选中节点，根据id筛选
                        if ($('#menuPid').val() != '0') treeSelect.checkNode('menu', $('#menuPid').val());
                    },
                    click: function (d) {
                        $('#menuPid').val(d.current.id);
                    }
                });
            }
            //
            app.initData = function (data) {
                form.val('val-filter', data);
                //
                app.loadDb();
                app.loadMenu();
                //
                var icon = data.menuIcon || 'layui-icon-star-fill';
                iconPicker.render({ elem: '#menuIcon', type: 'fontClass' });
                iconPicker.checkIcon('menuIcon', icon);
                if (data.generateMenu != undefined) $('#generateMenu').val(data.generateMenu + '');
                //
                form.on('select(configId)', function (data) {
                    if (data.value != '') {
                        app.loadTable(data.value);
                    }
                });
            };
        })
    </script>
}
